/**
 * Created by yuweimei on 2018/3/27.
 */
app.controller('personal_home', function($scope, $http) {

	//显示删除操作页面
	$scope.allShow = function(tourId) {
		$scope.tourId = tourId;
		$('.all').css("display", "block");
	};
	//隐藏删除操作页面
	$scope.allHide = function() {
		$('.all').css("display", "none");
	};

	$('.alert_top').click(function() {
		$('.all').css("display", "none");
	});

	var off = true;
	var star = $(".fa-star-o");
	var list_con2 = $('.list_con2');
	star.click(function() {
		if(star.off) {
			$('.all').css("display", "none");
			$.toast("已收藏");

			star.css("color", "#fba314");
			list_con2.text("取消收藏");
			star.off = false;
		} else {
			$('.all').css("display", "none");
			$.toast("已取消收藏");
			star.css("color", "#697480");
			list_con2.text("收藏");
			star.off = true;
		}
	});

	//证书
	var token = localStorage.getItem('token');
	//用户信息对象
	$scope.showCircle = false;
	$scope.userInfo = {};
	$scope.getPerInfo = function() {
		if(!token) {
			//判断有无证书
			$.toast("请先登录");
			location.href = '#login';
			return;
		}
		$http({
			method: 'get',
			url: app.netPath + '/dynamic/showTour',
			headers: {
				'Authorization': token
			}
		}).then(function(res) {
			if(!res.data.error) {
				$scope.userInfo = res.data.data;
				console.log(res.data.data);

				var avatarImg = {};

				if(res.data.data.userAvatarAddress) {
					avatarImg.imgAddress = app.netPath + '/resources/img/' + res.data.data.userAvatarAddress;
					$scope.userInfo.avatarImg = avatarImg;
				}

				if(res.data.data.userDetail) {
					$scope.userDetail = $scope.userInfo.userDetail;
				}

				$scope.userInfo.userSexWord = $scope.userInfo.userSex == 0 ? '男' : $scope.userInfo.userSex == 1 ? '女' : '点击选择';
				var index = $(".sex").text();
				if($scope.userInfo.userSex == 0) {
					$(".sex_boy").css("background", "blue");
					$(".sex_icon").addClass("fa-mars")
				} else {
					$(".sex_boy").css("background", "#fe8faf");
					$(".sex_icon").addClass("fa-venus")
				}

				var thisTime = new Date();
				$scope.userInfo.userAge = parseInt((parseInt(thisTime.valueOf()) - parseInt($scope.userInfo.userBirthday).valueOf()) / (365 * 24 * 60 * 60 * 1000));
				if($scope.userInfo.userNickName) {
					$scope.words = $scope.userInfo.userNickName;
				} else {
					$scope.words = "不一样的昵称，不一样的心情";
				}
				for(var i = 0; i < res.data.data.dynamicsList.length; i++) {
					//放置动态图片
					if(res.data.data.dynamicsList[i].tourdynaImg) {
						//得到每条动态的图片数组
						var dynamicImgs = app.getDynamicImg(res.data.data.dynamicsList[i].tourdynaImg);
						for(var j = 0; j < dynamicImgs.length; j++) {
							dynamicImgs[j] = app.netPath + '/resources/taskImg/' + dynamicImgs[j];
							app.buildImageUrl(dynamicImgs, j, function() {
								$scope.$digest();
							});
						}
						res.data.data.dynamicsList[i].tourdynaImg = dynamicImgs;
					}
					var lat = res.data.data.dynamicsList[i].positionX;
					var lng = res.data.data.dynamicsList[i].positionY;
					res.data.data.dynamicsList[i].distance = app.getGreatCircleDistance(lat, lng);
					if(res.data.data.dynamicsList[i].distance >= 1000) {
						res.data.data.dynamicsList[i].distance = (app.getGreatCircleDistance(lat, lng) / 1000).toFixed(2) + "千米";
					} else {
						res.data.data.dynamicsList[i].distance = app.getGreatCircleDistance(lat, lng) + "米";
					}
				}
			} else {
				$.toast(res.data.error + ',' + res.data.msg);
			}
		}, function() {
			$.toast('网络走丢了！')
		});

	};
	//删除发布的动态
	$scope.deleteATour = function() {
		if(!token) {
			//判断有无证书
			$.toast("请先登录");
			location.href = '#login';
			return;
		} else {
			$.confirm('确定删除？', '', function() {
				//欲删除的动态id
				var tourId = $scope.tourId;
				$http({
					method: 'get',
					url: app.netPath + '/dynamic/details/delete',
					headers: {
						'Authorization': token
					},
					params: {
						tourId: tourId
					}
				}).then(function(res) {
					if(!res.data.error) {
						$('.all').css("display", "none");
						$.toast('删除成功');
						//刷新动态列表
						$scope.getPerInfo();
					} else {
						$.toast(res.data.error + ',' + res.data.msg);
					}
				}, function() {
					$.toast('网络走丢了！')
				});
			}, function() {
				$.toast('已取消')
			});
		}
	};
	//跳转粉丝页面
	$scope.GoToLiker = function(userId) {
		//登录用户Id
		sessionStorage.setItem('MyUserId', userId);
	};
	//跳转关注页面
	$scope.GoToFollow = function(userId) {
		//登录用户Id
		sessionStorage.setItem('MyUserId', userId);
	};
	$scope.GoToReview = function(tourdynaId) {
		sessionStorage.setItem('tourdynaInfoId', tourdynaId);
	};
});